<template>
  <ul>
    <!-- //v-for="item in 10" -->
    <li v-for="(item, idnex) in state.data" :key="index" @click="hand(item)">
      <div class="header">
        <div class="comment">
          <img :src=item.avatar alt="头像" class="avatar">
          <div class="info">
            <p class="username">{{ item.commentator }}</p>
            <p class="content">{{ item.content }}</p>
            <div class="comment-footer">
              <p class="time">{{ item.c_time }} </p> <span><a class="like"><van-icon name="good-job-o" />{{ item.praise
                  }}</a>
                <a v-if="item.isReply" @click="statement(item)" class="collect"><van-icon name="chat-o" />取消</a>
                <a v-else @click="statement(item)" class="collect"><van-icon name="chat-o" />评论</a>
              </span>
              <a v-if="item.isShow && item.id === state.id" @click="dialogue(item) " class="dialogue"><van-icon
                  name="arrow-up" />收回</a>
              <a v-else @click="dialogue(item)" class="dialogue"><van-icon name="arrow-down" />对话</a>
            </div>
          </div>
        </div>
        <div class="module" v-if="item.isReply">
          <Statement :method="2" :item=item></Statement>
        </div>
      </div>


      <ul v-if="item.isShow && item.id === state.id">
        <li v-for="item in state.commentData" :key="item.id">
          <div class="comment comment-left">
            <img :src=item.replierAvatar alt="头像" class="avatar">
            <div class="info">
              <p class="username">{{ item.replierName }}</p>
              <p class="content">{{ item.replierContent }}</p>
              <div class="comment-footer">
                <p class="time">{{ item.c_time }}</p> <span><a class="like"><van-icon name="good-job-o" />1</a>
                  <a v-if="item.isReply" @click="statement(item)" class="collect"><van-icon name="chat-o" />取消</a>
                  <a v-else @click="statement(item)" class="collect"><van-icon name="chat-o" />评论</a></span>
              </div>
            </div>
          </div>
          <div class="module" v-if="item.isReply" @click="handle(item)">
            <Statement :method="2" :item=item></Statement>
          </div>
        </li>
      </ul>
    </li>
  </ul>
</template>

<script setup>
import Statement from '@/components/Statement.vue'
import { ref } from 'vue';
import { useRoute } from 'vue-router';
import axios from '@/api/index.js';
import { onMounted } from 'vue';
import { reactive } from 'vue';

const hand = (item) => {
  // console.log(item);
}

const handle = (item) => {
  console.log(item);
}

const route = useRoute()

const state = reactive({
  data: '',
  commentData: [],
  id: false
})



const id = route.query.id



// console.log(id);
// 点击切换，查看对话

const dialogue = async (item) => {
  console.log(item);
  state.data.forEach((comment) => {
    comment.isShow = false; // 收起所有对话
  });
  item.isShow = true; // 展开当前点击的对话
  state.id = item.id; // 记录当前点击的对话id

  // 获取本篇文章的二级评论
  const result1 = await axios.post('/comment/findSubmitSecondaryComment', {
    commentId: item.name
  });
  console.log(result1.data);
  if (result1.data) {
    state.commentData = result1.data;
  }
};

// 点击收回
const collapse = (item) => {
  item.isShow = false; // 只收回当前点击的对话
};

// 点击评论
const statement = (item) => {
  item.isReply = !item.isReply;
  console.log(item);
};

// 获取本篇文章的评论
onMounted(async () => {
  const result = await axios.post('/comment/findComment', {
    id: id
  });
  state.data = result.data;

  console.log(result.data);
});

</script>

<style lang="less" scoped>
.comment {
  width: 100%;
  margin: 0 auto;
  padding: 20px 5px;
  background-color: #f5f5f5;
  border-radius: 5px;
  display: flex;
  align-items: center;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
}

.username {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 5px;
}

.content {
  font-size: 14px;
  line-height: 1.5;
  margin-bottom: 10px;
}

.time {
  font-size: 14px;
  color: #999;
  text-align: center;
  margin: auto;
}

.comment-footer {
  display: flex;
  justify-content: space-between;
  text-align: center;
  font-size: 15px;
  color: #999;
}

.comment-left {
  padding-left: 20px;
  box-sizing: border-box;
}
</style>